home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Presentations / Presentations ’92 / PatchWorks Kit / <PatchWorks++> / VirtualWorld.cp < prev    next >
Encoding:
Text File  |  1992-04-27  |  2.2 KB  |  109 lines  |  [TEXT/MPS ]

  1. /*
  2.     VirtualWorld.cp
  3.     
  4.     Implementation of Virtual world maintenance class.
  5.     
  6.     This class maintains an A5 global space, as well as supporting
  7.     initialization of virtual function tables at run-time.
  8.     
  9.     by Patrick Beard.
  10.     
  11.     based on TN 256.
  12.  
  13.     ©1990 by Patrick C. Beard.  All rights reserved.
  14.  */
  15.  
  16. #ifndef __VIRTUALWORLD__
  17. #include "VirtualWorld.h"
  18. #endif
  19. #ifndef __EXCEPTIONS__
  20. #include "Exceptions.h"
  21. #endif
  22.  
  23. #ifndef __QUICKDRAW__
  24. #include <QuickDraw.h>
  25. #endif
  26. #ifndef __ERRORS__
  27. #include <Errors.h>
  28. #endif
  29.  
  30. extern "C" {
  31. void A5Init(void*);        // function that sets up (initializes) the A5 world.
  32. long A5Size(void);        // function that returns the size of our A5 world.
  33. void init_vtbls(void);    // function that initializes virtual function tables.
  34.  
  35. // utility routine to switch A5's.  (lifted from OSUtils.h)
  36. #pragma parameter __A0 SwapA5(__A0)
  37. void* SwapA5(void* newA5) = 0xC14D;
  38. }
  39.  
  40.  
  41. // constructor:  set up the current code's required A5 world.
  42.  
  43. VirtualWorld::VirtualWorld(Boolean worldFloats)
  44. {
  45.     itsFloating = worldFloats;
  46.     itsSize = A5Size();                            // store our size for speed.
  47.     itsWorld = new char[itsSize];                // allocate storage for our globals.
  48.     
  49.     // initialize our globals.
  50.     itsA5 = itsWorld + itsSize - 32;            // see TN 256.
  51.     A5Init(itsA5);
  52.     
  53.     // call InitGraf to initialize quickdraw globals &  get a valid port.
  54.     GrafPtr port;
  55.     GetPort(&port);
  56.     
  57.     void* oldWorld = Enter();                    // go inside our world.
  58.     InitGraf(&qd.thePort);
  59.     SetPort(port);
  60.     
  61.     // if this code doesn't float, only call this function once.
  62.     if (!itsFloating)
  63.         init_vtbls();
  64.         
  65.     Leave(oldWorld);                            // leave the virtual world.
  66. }
  67.  
  68. VirtualWorld::~VirtualWorld()
  69. {
  70.     delete itsWorld;
  71. }
  72.  
  73. void* VirtualWorld::Enter()
  74. {
  75.     GrafPtr currPort;
  76.     GetPort(&currPort);
  77.  
  78.     void* oldA5 = SwapA5(itsA5);
  79.     
  80.     SetPort(currPort);
  81.  
  82.     if (itsFloating) {
  83.         // call virtual table initialization functions everytime our world is
  84.         // entered.  this way, if we've been relocated, the tables will be correct.
  85.         init_vtbls();
  86.     }
  87.     
  88.     return oldA5;
  89. }
  90.  
  91. void VirtualWorld::Leave(void* oldA5)
  92. {
  93.     SwapA5(oldA5);
  94. }
  95.  
  96. void* operator new(size_t n)
  97. {
  98.     void* p = NewPtr(n);
  99.     if (!p)
  100.         throw(memFullErr);
  101.     return p;
  102. }
  103.  
  104. void operator delete(void* p)
  105. {
  106.     if (p)
  107.         DisposPtr((Ptr)p);
  108. }
  109.